/**
 * The SimpleProducer class consists only of a main method,
 * which sends several messages to a queue or topic.
 *
 */
import javax.jms.*;
import javax.naming.*;


public class UpdateQueueProducer {
    /**
     * Main method.
     *
     * @param args     the destination used by the example
     *                 and, optionally, the number of
     *                 messages to send
     */
    public static void main(String[] args) {
        /*
         * Create a JNDI API InitialContext object if none exists
         * yet.
         */
        Context jndiContext = null;

        try {
            jndiContext = new InitialContext();
        } catch (NamingException e) {
            System.out.println("Could not create JNDI API context: " +
                e.toString());
            System.exit(1);
        }

        /*
         * Look up connection factory and destination.  If either
         * does not exist, exit.  If you look up a
         * TopicConnectionFactory or a QueueConnectionFactory,
         * program behavior is the same.
         */
        ConnectionFactory connectionFactory = null;
        Destination dest = null;

        try {
            connectionFactory = (ConnectionFactory) jndiContext.lookup(
                    "jms/ConnectionFactory");
            dest = (Destination) jndiContext.lookup("UpdateQueue");
        } catch (Exception e) {
            System.out.println("JNDI API lookup failed: " + e.toString());
            e.printStackTrace();
            System.exit(1);
        }

        /*
         * Create connection.
         * Create session from connection; false means session is
         * not transacted.
         * Create producer and text message.
         * Send messages, varying text slightly.
         * Send end-of-messages message.
         * Finally, close connection.
         */
        Connection connection = null;
        MessageProducer producer = null;

        try {
            connection = connectionFactory.createConnection("guest","guest");

            Session session =
                connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            producer = session.createProducer(dest);

            TextMessage message = session.createTextMessage();

//            for (int i = 0; i < 3; i++) {
//                message.setText("This is message " + (i + 1));
//                System.out.println("Sending message: " + message.getText());
//                producer.send(message);
//            }
            String text = "<C4Message>\n\t<TroubleTicketResponse uid=\"-332639492\"/>\n</C4Message>";
//            String text = 	"<C4Message>\n\t<Update name=\"Install\" uid=\"785125431\">\n\t\t" +
//							"<Param name=\"Status\" value=\"Success\"/>\n\t</Update>\n</C4Message>";
    		message.setText(text);
            producer.send(message);
            
            System.out.println("Sending message..." + text);

            /*
             * Send a non-text control message indicating end of
             * messages.
             */
            producer.send(session.createMessage());
        } catch (JMSException e) {
            System.out.println("Exception occurred: " + e.toString());
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (JMSException e) {
                }
            }
        }
    }
}